package com.cl.utils;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.collections.map.LinkedMap;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jca.cci.connection.ConnectionFactoryUtils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

 

public class DBUtils {
	
	//private static Logger log = LoggerFactory.getLogger(DBUtils.class);
	
	public static List<Map<String,Object>> queryForList(String sql){
		Connection conn = null;
		PreparedStatement ps = null;
		List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
		LinkedMap retMap = null;
		try {
			conn = ConnectionPool.getInstance().getConnection();
			//log.debug("EXEC SQL:"+sql);
			ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();
			while(rs.next()){
				retMap = new LinkedMap();
				for( int i=1;i<=columnCount;i++ ){
					retMap.put(rsmd.getColumnName(i), rs.getObject(rsmd.getColumnName(i)));	
				}
				retList.add(retMap);
			}
			rs.close();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
        	//log.error("执行【"+sql+"】语句异常:"+e.getMessage());
		}finally{
			try {
				if(conn!=null){
					ConnectionPool.freeConnection(conn);//释放，但并未断开连接
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
        } 
		return retList;
	} 
	
	/**
     * 数据库连接使用后关闭
     * 插入操作返回主键
     * @param sql
     * @param latnId
     * @return long
     */
    public  static void insert(String sql) throws SQLException{ 
    	Connection conn = null;
		PreparedStatement ps = null;
        try {
        	conn = ConnectionPool.getInstance().getConnection();
            //log.debug("EXEC SQL:"+sql);
            ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);  
            ps.executeUpdate();
           if(ps != null){
            ps.close();
           }
        } catch (SQLException e) {  
        	e.printStackTrace(); 
        	//log.error("连接【"+"】执行【"+sql+"】语句异常:"+e.getMessage());
        	throw new SQLException("连接【"+"】执行【"+sql+"】语句异常:"+e.getMessage());
        }finally{
			try {
				if(ps != null){
					ps.close();
				}
				if(conn!=null){
					ConnectionPool.freeConnection(conn);//释放，但并未断开连接
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
        } 
        
    }
    /**
     * 数据库连接使用后关闭
     * 更新操作
     * @param sql
     * @param latnId
     */
    public static void update(String sql) throws SQLException{ 
    	Connection conn = null;
    	PreparedStatement pstmt = null;
        try {
        	conn = ConnectionPool.getInstance().getConnection();
        	//log.debug("EXEC SQL:"+sql);
        	pstmt = conn.prepareStatement(sql);
        	pstmt.executeUpdate();
        } catch (SQLException e) {
        	e.printStackTrace();
        	//log.error("连接【"+"】执行【"+sql+"】语句异常:"+e.getMessage());
        	throw new SQLException("连接【"+"】执行【"+sql+"】语句异常:"+e.getMessage());
        } finally{
			try {
				if(pstmt != null){
					pstmt.close();
				}
				if(conn!=null){
					ConnectionPool.freeConnection(conn);//释放，但并未断开连接
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
        } 
    }  
    /**
     * 数据库连接使用后关闭连接
     * 删除操作
     * @param sql
     * @param latnId
     */
    public  static void delete(String sql) throws SQLException{
    	
    	Connection conn = null;
    	PreparedStatement pstmt = null;
        try {
        	conn = ConnectionPool.getInstance().getConnection();
        	//log.debug("EXEC SQL:"+sql);
            pstmt = conn.prepareStatement(sql);
            pstmt.executeUpdate();
        } catch (SQLException e) {  
        	e.printStackTrace();
        	//log.error("连接【"+"~~~"+"】执行【"+sql+"】语句异常:"+e.getMessage());
        	throw new SQLException("连接【"+"~~~"+"】执行【"+sql+"】语句异常:"+e.getMessage());
        } 
        finally{
			try {
				if(pstmt != null){
					pstmt.close();
				}
				if(conn!=null){
					ConnectionPool.freeConnection(conn);//释放，但并未断开连接
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
        } 
    }    
    
}
